﻿Public Class xtf_MateriaPrima_ 
    Dim obj_articulo As cls_articulo_
    Dim dtt_general As New DataTable

    Public Sub New(ByRef prm_articulo As cls_articulo_)
        InitializeComponent()
        obj_articulo = prm_articulo
        Me.Text = "Arbol de composicion - " & obj_articulo.ToString
        Me.btn_Guardar.Visible = False
        'Prepara grid y datasource proveedorarticulo----------------------------------------------------
        gdv_general.Tag = New Collections.Generic.List(Of cls_proveedorarticulo_)
        dtt_general = New DataTable
        dtt_general.Columns.Add("idarticulo", GetType(Integer))
        dtt_general.Columns.Add("Materia Prima", GetType(Object))
        dtt_general.Columns.Add("Valor Segun", GetType(String))
        dtt_general.Columns.Add("Proveedor", GetType(Object))
        dtt_general.Columns.Add("Fecha", GetType(DateTime))
        dtt_general.Columns.Add("Valor", GetType(Decimal))
        dtt_general.Columns.Add("Costo Bloqueado", GetType(Boolean))

        gdc_general.DataSource = dtt_general
        gdc_general.ForceInitialize()

        gdv_general.PopulateColumns()
        gdv_general.Columns("idarticulo").Visible = False
        '-----------------------------------------------------------------------------------------------
        CargarListado()
    End Sub

    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub CargarListado()
        CrearRows2(obj_articulo, dtt_general, Nothing)
        gdv_general.BestFitColumns()
    End Sub

    Private Sub CrearRows2(ByRef prm_articulo As cls_articulo_, ByRef prm_dtt As DataTable, Optional prm_articulocomposicionarticulo As cls_articulocomposicionarticulo_ = Nothing)
        Dim MiROW As DataRow = prm_dtt.NewRow
        MiROW("Materia Prima") = prm_articulo

        If Not IsNothing(prm_articulocomposicionarticulo) Then
            MiROW("Valor") = prm_articulo.costo
        Else
            MiROW("Valor") = prm_articulo.costo
        End If

        MiROW("Valor Segun") = EnumDescription(prm_articulo.tipocosto)
        MiROW("Proveedor") = prm_articulo.proveedor
        MiROW("Fecha") = Now.Date
        MiROW("Costo Bloqueado") = prm_articulo.costobloqueado
        If prm_articulo.tipo = cls_articulo_.enu_tipo.Mprima Then
            Dim agregar As Boolean = True
            For Each row As DataRow In dtt_general.Rows
                If CType(row("Materia Prima"), cls_articulo_).idarticulo = prm_articulo.idarticulo Then
                    agregar = False
                End If
            Next
            If agregar Then
                prm_dtt.Rows.Add(MiROW)
            End If
        End If

        If Not IsNothing(prm_articulo.articulocomposicionarticulo_) Then
            If Not IsNothing(prm_articulo.articulocomposicionarticulo_.Items) Then
                For Each comp As cls_articulocomposicionarticulo_ In prm_articulo.articulocomposicionarticulo_.Items
                    CrearRows2(comp.articulo, prm_dtt, comp)
                Next
            End If
        End If
    End Sub

   
End Class